package com.right.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.right.entity.Cart;
import com.right.entity.Foods;
import com.right.entity.Orderdetail;
import com.right.service.FoodsService;
import com.right.service.OrderdetailService;
import com.right.service.impl.FoodsServiceImpl;
import com.right.service.impl.OrderdetailServiceImpl;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/OrderdetailServlet")
public class OrderdetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrderdetailService os = new OrderdetailServiceImpl();
	FoodsService foodsService = new FoodsServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderdetailServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("add".equals(op)) {

		} else if ("deleteCartByKey".equals(op)) {
			// 获取详情编号编号
			int key = Integer.parseInt(request.getParameter("key"));
			System.out.println(key);
			HttpSession session = request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			if (cart != null) {
				HashMap<Integer, Orderdetail> cartItems = cart.getCartItems();
				// 修改总价
				Orderdetail cartItem = cartItems.get(key);
				cart.setTotal(cart.getTotal() - cartItem.getTotal());
				// 删除
				cartItems.remove(key);
				cart.setCartItems(cartItems);
			}

			session.setAttribute("cart", cart);
			request.getRequestDispatcher("cart.jsp").forward(request, response);

		} else if ("update".equals(op)) {
			// 获取详情编号编号
			int key = Integer.parseInt(request.getParameter("key"));
			System.out.println(key);
			HttpSession session = request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			if (cart != null) {
				HashMap<Integer, Orderdetail> cartItems = cart.getCartItems();
				// 修改总价
				Orderdetail cartItem = cartItems.get(key);
				String sign = request.getParameter("sign");
				if("up".equals(sign)) {
					cart.setTotal(cart.getTotal() + cartItem.getFoods().getPrice());
					cartItem.setCount(cartItem.getCount()+1);
					cartItem.setTotal(cartItem.getTotal()+cartItem.getFoods().getPrice());
				}else if ("drop".equals(sign)) {
					if (cartItem.getCount()>1) {
						cart.setTotal(cart.getTotal() - cartItem.getFoods().getPrice());
						cartItem.setCount(cartItem.getCount()-1);
						cartItem.setTotal(cartItem.getTotal()-cartItem.getFoods().getPrice());
					}
					
				}
				
			}

			session.setAttribute("cart", cart);
			request.getRequestDispatcher("cart.jsp").forward(request, response);

		} else if ("addFoodsToCart".equals(op)) {

			// 获取菜品编号
			int foodsid = Integer.parseInt(request.getParameter("foodsid"));
			System.out.println(foodsid);
			// 购买数量
			int count = 1;
			if (request.getParameter("count") != null) {
				count = Integer.parseInt(request.getParameter("count"));
			}
			System.out.println(count);

			Foods foods = foodsService.getFoodsByFoodsid(foodsid);
			System.out.println(foods);
			HttpSession session = request.getSession();
			Cart cart = (Cart) session.getAttribute("cart");
			if (cart == null) {
				cart = new Cart();
			}
			Orderdetail orderdetail = new Orderdetail();
			orderdetail.setCount(count);
			orderdetail.setFoods(foods);
			// 计算小计
			double subTotal = foods.getPrice() * count;
			orderdetail.setTotal(subTotal);

			HashMap<Integer, Orderdetail> cartItems = cart.getCartItems();

			double newSubTotal = 0;
			if (cartItems.containsKey(foodsid)) {
				Orderdetail item = cartItems.get(foodsid);
				// 修改数量
				int oldBuyNum = item.getCount();
				oldBuyNum = oldBuyNum + count;
				item.setCount(oldBuyNum);
				// 修改小计
				double oldSubTotal = item.getTotal();
				newSubTotal = count * foods.getPrice();
				oldSubTotal = oldSubTotal + newSubTotal;
				item.setTotal(oldSubTotal);
			} else {
				cartItems.put(foodsid, orderdetail);
				newSubTotal = count * foods.getPrice();
			}

			// 计算 总计
			double total = cart.getTotal() + newSubTotal;
			cart.setTotal(total);
			cart.setCartItems(cartItems);
			session.setAttribute("cart", cart);
			// System.out.println(cart);
			request.getRequestDispatcher("cart.jsp").forward(request, response);

		} else if ("selectByPage".equals(op)) {

		} else if ("login".equals(op)) {

		} else if ("".equals(op)) {

			request.getRequestDispatcher("main.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
